package your.packag;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends TabActivity implements TabHost.TabContentFactory {
	Intent intent; 
	ProgressDialog dialog;

	RadioButton rbFirst;
    
    RadioButton rbSecond ; 
     
    RadioButton rbThird ;
    RadioButton rbFourth ;
    RadioButton rbFifth;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smartlive);
        TabHost tabHost = getTabHost();
        intent = new Intent().setClass(this, FinancialActivity.class);
        tabHost.addTab(tabHost.newTabSpec("finacial").setIndicator("Finacial").setContent(intent));
        intent = new Intent().setClass(this, HistoryActivity.class);
        tabHost.addTab(tabHost.newTabSpec("history").setIndicator("History").setContent(intent));
        intent = new Intent().setClass(this,ReportActivity.class);
        tabHost.addTab(tabHost.newTabSpec("report").setIndicator("Report").setContent(intent));
        intent = new Intent().setClass(this, BudgetActivity.class);
        tabHost.addTab(tabHost.newTabSpec("budget").setIndicator("Budget").setContent(intent))  ;
        intent = new Intent().setClass(this, ManagementActivity.class);
        tabHost.addTab(tabHost.newTabSpec("management").setIndicator("Management").setContent(intent))  ;
        
        dialog = new ProgressDialog(this);
        /*dialog = ProgressDialog.show(this, "", 
                "Loading. Please wait...", true);*/

        setupUI();
    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);
	MenuInflater inflater = getMenuInflater();
	inflater.inflate(R.layout.menu, menu);
	return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	// Handle item selection
	switch (item.getItemId()) {
	case R.id.new_game:
		Intent intent = new Intent(this,LoginActivity.class);
		startActivity(intent);
	default:
	return super.onOptionsItemSelected(item);
	}
	}
	public View createTabContent(String tag) {
       // LayoutInflater inflater = getLayoutInflater();

       /* if (tag.equals("finacial")) {
            convertView=inflater.inflate(R.layout.financial, null, false);
        } else if (tag.equals("history")) {
            convertView=inflater.inflate(R.layout.history, null, false);
        } else if (tag.equals("report")) {
            convertView=inflater.inflate(R.layout.report, null, false);
        } else if (tag.equals("budget")) {
            convertView=inflater.inflate(R.layout.budget, null, false);
        }
          else if (tag.equals("management")) {
            convertView=inflater.inflate(R.layout.management, null, false);
        }*/
        return null;    
    }
	private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	dialog.dismiss();
        }
	};
	private Handler setTabhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	
        	getTabHost().setCurrentTab(msg.arg1);
        }
	};
	public int tab=0;
	private void setcurrentTab(final int i)
	{
		dialog = ProgressDialog.show(this, "", 
                "Loading. Please wait...", true);
		
		
		new Thread(new Runnable() {
			
			  public void run() {
				  setTabhandler.sendEmptyMessage(0);
	
				  Message msg = new Message();
				  msg.arg1 = i;
				  setTabhandler.sendMessage(msg);
				handler.sendEmptyMessage(0);
			  }
		}).start();
		
		
	}
	RadioButton[] rdTabs = new RadioButton[5];
	private void setTab(int index)
	{
		getTabHost().setCurrentTab(index);
		for(int i = 0;i<rdTabs.length;i++)
		{
			if(i == index)
			{
				rdTabs[i].setBackgroundColor(Color.LTGRAY);
			}
			else
			{
				rdTabs[i].setBackgroundColor(Color.DKGRAY);
			}
		}
	}
	private void setupUI() {

		
		rdTabs[0] = (RadioButton) findViewById(R.id.first);
         
		rdTabs[1] = (RadioButton) findViewById(R.id.second); 
         
		rdTabs[2] = (RadioButton) findViewById(R.id.third);
		rdTabs[3] = (RadioButton) findViewById(R.id.fourth);
		rdTabs[4] = (RadioButton) findViewById(R.id.fifth);
         
		rdTabs[0].setButtonDrawable(R.drawable.financial);
		rdTabs[1].setButtonDrawable(R.drawable.history);  
		rdTabs[2].setButtonDrawable(R.drawable.chart);
		rdTabs[3].setButtonDrawable(R.drawable.budget);
		rdTabs[4].setButtonDrawable(R.drawable.management);
        
		for(int i = 0;i<rdTabs.length;i++)
		{
			if(i == 0)
			{
				rdTabs[i].setBackgroundColor(Color.LTGRAY);
			}
			else
			{
				rdTabs[i].setBackgroundColor(Color.DKGRAY);
			}
		}
         
        RadioGroup rg = (RadioGroup) findViewById(R.id.states);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, final int checkedId) {

                switch (checkedId) {
                
                case R.id.first:
                {
                    setTab(0);
                    break;
                }
                case R.id.second:
                {

                    setTab(1);
                    break;
                }
                case R.id.third:
                {

                    setTab(2);
                    break;
                }
                case R.id.fourth:
                {

                    setTab(3);
                    break;
                }
                case R.id.fifth:
                {

                    setTab(4);
                    
                    break;
                }
                
                }
            }
        });
    }

}
